package com.org.manhu.chargehelper.update;

import java.text.DecimalFormat;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.StrictMode;

/**
 * @文件名：UpdateUtil.java
 * @包名：com.coward.callbackwebservice
 * @作者：李朋辉 @QQ：892839211
 * @创建时间：2015年2月28日 上午9:28:23
 *
 * @描述：更新工具类
 */
@SuppressLint("NewApi")
public class UpdateUtil {

	/**
	 * @描述：检查网络是否连
	 * @param context
	 * @return
	 */
	public static boolean checkNetwork(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm == null) {
			return false;
		} else {
			NetworkInfo[] info = cm.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public static void initStrictMode(Context context) {
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath()
				.build());
	}

	/**
	 * 获取软件版本号
	 *
	 * @param context
	 * @return
	 */
	public static int getVersionCode(Context context) {
		int versionCode = 0;
		try {
			versionCode = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0).versionCode;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return versionCode;
	}

	/**
	 * @描述：将文件大小转化为MB
	 * @param input
	 * @return
	 */
	public static String convertToMB(int input) {
		String out = "";
		DecimalFormat df = new DecimalFormat("0.00");
		Float size = input / (1024 * 1024.0f);
		out = df.format(size);
		return out + "M";
	}
	
}
